<template>
  <div class="app-container index-box" ref="index">
    <div class="search" ref="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">

      </el-form>
     </div>
     <div class="activeBox" ref="activeBox">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
        </el-row>
     </div>
     <div class="table">
        <el-table
           border
          v-if="refreshTable"
          v-loading="loading"
          :data="menuList"
          :height="tableHeight"
          row-key="columnId"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column prop="columnName" label="菜单名称" :show-overflow-tooltip="true" width="280"></el-table-column>
          <el-table-column prop="icon" label="图片" align="center" width="100">
            <template slot-scope="scope">
               <el-image :src="scope.row.columnImg" fit="fill" v-image-preview="scope.row.columnImg" class="img"   v-if="scope.row.columnImg"></el-image>
            </template>
          </el-table-column>
          <el-table-column label="状态"  align="center">
            <template #default="scope">
              <div class="d-flex justify-center">
                <MySwitch :value.sync="scope.row.status" :id="scope.row.status" text="启用|禁用" textValue="1|2" @changeStatus="handleChangeStatus(scope.row)" :permission="true"></MySwitch>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
             <el-button size="medium"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="medium"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"

              >新增</el-button>
             <!-- <el-button
              	v-if="!scope.row.children"
                size="medium"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                 v-has="'system:menu:delt'"
              >删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
     </div>


    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" v-dialogDrag :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单">
              <treeselect
                v-model="form.parentId"
                :options="menuList"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="名称" prop="columnName">
              <el-input v-model="form.columnName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="链接" prop="goodsUrl">
              <el-input v-model="form.goodsUrl" placeholder="请输入菜单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="columnImg">
             <MyUpImg :imagePreview='form.columnImg'  @change="imageChange($event, 'columnImg')"></MyUpImg>
            </el-form-item>
          </el-col>



        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="this.open = false" size="small">重 置</el-button>
        <el-button type="primary" @click="submitForm" size="small" :loading="userLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getActivityType, addActivityColumn, activityGoodsupdateStatus} from '@/api/getData';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
// import IconSelect from "@/components/IconSelect";

export default {
  name: "Menu",
  //dicts: ['sys_show_hide', 'sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      userLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      //table 高度
      tableHeight:500,
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined
      },
      // 表单参数
      form: {
        parentId:'',
        columnId:'',
        columnName:'',
        goodsUrl:'',
        columnImg:'',
      },
      // 表单校验
      rules: {
        columnName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
    	this.tableHeight = this.$refs.index.offsetHeight - this.$refs.search.offsetHeight - this.$refs.activeBox.offsetHeight-45;
    })
  },
  methods: {
    imageChange(path, name) {
    	this.form[name] = path
    },
    async handleChangeStatus(row) {
      try {
        console.log(row.status)
        let data = {
          columnId: row.columnId,
          status: row.status,
        }
        const result = await activityGoodsupdateStatus(data);
        if (result.code == 200) {
          this.$message.success(result.msg);
        } else {
          this.$message.error(result.msg);
          row.status = !row.status;
        }
      } catch (err) {
        console.log(err)
      }
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      getActivityType().then(response => {
         this.menuList = response.data;
         this.loading = false;
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.columnId,
        label: node.columnName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      if(row){
        this.form = {
          parentId:row.parentId+''
        };
      }else{
        this.form.parentId = undefined;
      }
      this.open = true;
      this.title = "添加菜单";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
     // this.reset();
     // this.getTreeselect();
     this.form = {
       parentId:row.parentId+'',
       columnId:row.columnId,
       columnName:row.columnName,
       goodsUrl:row.goodsUrl,
       columnImg:row.columnImg,
     };
     this.open = true;
     this.title = "修改菜单";
      // getMenu(row.menuId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改菜单";
      // });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.userLoading = true;
          addActivityColumn(this.form).then(response => {
            if(response.code == 200){
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
            }else{
               this.$modal.msgError(response.msg);
            }
            this.userLoading = false;
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.menuName + '"的数据项？').then(function() {
        return delMenu(row.menuId);
      }).then((res) => {
        if(res.code == 200){
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }else{
           this.$modal.msgError(res.msg);
        }

      }).catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>

</style>
